package com.tg.app.widget;

import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.demo.view.TGBottomSheetDialog;
import com.tg.app.R;

import java.util.List;

/**
 * 底部的单选样式的弹窗
 */
public class BottomRadioButtonSheetListDialog extends TGBottomSheetDialog {

    private BottomSheetAdapter adapter;
    private BottomSheetClickListener listener;

    private TextView titleTv;

    public BottomRadioButtonSheetListDialog(@NonNull Context context) {
        super(context);
    }

    public void setClickListener(@NonNull BottomSheetClickListener listener) {
        this.listener = listener;
    }


    public void setOnDismissListener(DialogInterface.OnDismissListener listener) {
        bottomSheetDialog.setOnDismissListener(listener);
    }

    @Override
    protected int getContentView() {
        return R.layout.layout_bottom_radio_sheet;
    }

    @Override
    protected void initView(@NonNull View view) {

        titleTv = view.findViewById(R.id.bottom_sheet_desc_tv);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);

        RecyclerView recyclerView = view.findViewById(R.id.bottom_sheet);
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new BottomSheetAdapter();
        recyclerView.setAdapter(adapter);

        bottomSheetDialog.getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundResource(android.R.color.transparent);

        view.findViewById(R.id.bottom_sheet_exit).setOnClickListener(v -> dismiss());
    }

    public void setTitle(String title) {
        if (titleTv != null) {
            titleTv.setText(title);
        }
    }

    public void setData(@NonNull List<BottomSheetItem> bottomSheetItem) {
        adapter.setBottomSheetItem(bottomSheetItem);
        adapter.notifyDataSetChanged();
    }

    public void setSelectedIndex(int index) {
        adapter.setSelectedIndex(index);
        adapter.notifyDataSetChanged();
    }

    public void setNoneSelected() {
        setSelectedIndex(-1);
    }

    public static class BottomSheetItem {
        public int action;
        public String text;

        public BottomSheetItem(int action, String text){
            this.action = action;
            this.text = text;
        }
    }

    class BottomSheetAdapter extends RecyclerView.Adapter<BottomSheetViewHolder> {
        private List<BottomSheetItem> bottomSheetItems;
        private int mSelectedIndex = -1;
        private Context mContext;

        public void setSelectedIndex(int selectedIndex) {
            this.mSelectedIndex = selectedIndex;
        }

        public void setBottomSheetItem(List<BottomSheetItem> bottomSheetItem) {
            this.bottomSheetItems = bottomSheetItem;
        }

        @NonNull
        @Override
        public BottomSheetViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            mContext = parent.getContext();
            View view = LayoutInflater.from(mContext).inflate(R.layout.layout_bottom_radio_sheet_list_item, parent, false);
            return new BottomSheetViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull BottomSheetViewHolder holder, int position) {

            BottomSheetItem item = bottomSheetItems.get(position);
            if (item == null) return;

            holder.textView.setText(item.text);

            if (mSelectedIndex == position) {
                holder.imageView.setBackgroundResource(R.drawable.tg_radio_button_seleteted);
            } else {
                holder.imageView.setBackgroundResource(R.drawable.tg_radio_button_not_seleteted);
            }

            if (position == getItemCount() - 1) {
                holder.splitLine.setVisibility(View.GONE);
            } else {
                holder.splitLine.setVisibility(View.VISIBLE);
            }

            holder.itemView.setOnClickListener(v -> {
                bottomSheetDialog.dismiss();
                if (listener != null) {
                    assert item.text != null;
                    listener.onClick(item.action, item.text);
                }
            });
        }

        @Override
        public int getItemCount() {
            return bottomSheetItems == null ? 0 : bottomSheetItems.size();
        }
    }

    public interface BottomSheetClickListener {
        void onClick(int action, String text);
    }

    public static class BottomSheetViewHolder extends RecyclerView.ViewHolder {

        ImageView imageView;
        TextView textView;
        View splitLine;

        public BottomSheetViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.bottom_sheet_title);
            imageView = itemView.findViewById(R.id.bottom_sheet_icon);
            splitLine = itemView.findViewById(R.id.split_line);
        }
    }
}
